package com.tangding.eventBus;
import java.io.Serializable;
import java.util.concurrent.Executors;


import com.google.common.base.Strings;
import com.google.common.eventbus.Subscribe;
import com.tangding.common.event.MoonBaseEvent;
import com.tangding.common.eventbus.EventBus;
import com.tangding.common.eventbus.MoonEventBus;
import com.tangding.net.tcp.GameThreadFactory;

/**
 * @description 
 *
 * @author Ranger
 * @version 2017年5月4日_下午8:38:51
 *
 */
public abstract class EventBusPool {
	
	protected Pair<EventBus, EventBus> buildPairBus(Serializable key) {
		EventBus eventBusRequest = new MoonEventBus(key + " request", Executors.newSingleThreadExecutor(new GameThreadFactory("user eventBus request " + key, true)));
		EventBus eventBusResponse = new MoonEventBus(key + " response", Executors.newSingleThreadExecutor(new GameThreadFactory("user eventBus response " + key, true)));
		Pair<EventBus, EventBus> pair = new Pair<EventBus, EventBus>(eventBusRequest, eventBusResponse);
		eventBusRequest.register(new Object(){
			@Subscribe
			public void listen(MoonBaseEvent event) {
				event.onEvent();
			}
		});
		
		eventBusResponse.register(new Object(){
			@Subscribe
			public void listen(MoonBaseEvent event) {
				event.onEvent();
			}
		});
		return pair;
	}
	
	public abstract EventBus get(String uid, UserEventType type);
	public abstract void remove(String uid);

	public void post(String uid, MoonBaseEvent event, UserEventType type) {
		if (Strings.isNullOrEmpty(uid)) {
			return;
		}
		get(uid, type).post(event);
	}
}